package ui;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

import org.eclipse.swt.*;
import org.eclipse.swt.events.*;
import org.eclipse.swt.layout.*;
import org.eclipse.swt.widgets.*;

import org.eclipse.ui.forms.widgets.FormToolkit;
import storage.*;
import commands.Help;
import logic.TaskDirector;

public class GUI extends Composite {

	private final FormToolkit toolkit = new FormToolkit(Display.getCurrent());
	private Text outputTextField_;
	private Table table_;
	private Text inputTextField_;
	private TaskDirector logic_;
	private DateTime dateTime1_;
	private Button btnNewButton_;
	private int welcomeDisappear = 0;
	private float width = 718;
	private float height = 432;
	private static final String WELCOME_MESSAGE = "Welcome to Task Master";

	/**
	 * Create the composite.
	 * 
	 * @param parent
	 * @param style
	 */

	public GUI(Composite parent, int style, final Display display) {
		super(parent, style);
		logic_ = new TaskDirector();
		addDisposeListener(new DisposeListener() {
			public void widgetDisposed(DisposeEvent e) {
				toolkit.dispose();
			}
		});

		toolkit.adapt(this);
		toolkit.paintBordersFor(this);
		setLayout(new FormLayout());

		final Composite composite = new Composite(this, SWT.BORDER | SWT.RESIZE);
		FormData fd_composite = new FormData();
		fd_composite.bottom = new FormAttachment(0, 460);
		fd_composite.right = new FormAttachment(0, 780);
		fd_composite.top = new FormAttachment(0);
		fd_composite.left = new FormAttachment(0);
		composite.setLayoutData(fd_composite);
		composite.setToolTipText("TASK MASTER");
		toolkit.adapt(composite);
		toolkit.paintBordersFor(composite);
		composite.setLayout(new FormLayout());

		inputTextField_ = new Text(composite, SWT.BORDER);
		inputTextField_.addMouseListener(new MouseAdapter() {
			public void mouseDown(MouseEvent e) {
				if (welcomeDisappear < 1) {
					inputTextField_.setText("");
					welcomeDisappear++;
				}
			}
		});
		FormData fd_text = new FormData();
		fd_text.left = new FormAttachment(0, 10);
		fd_text.right = new FormAttachment(100, -10);
		fd_text.bottom = new FormAttachment(0, 47);
		fd_text.top = new FormAttachment(0, 10);
		inputTextField_.setLayoutData(fd_text);
		inputTextField_
				.setFont(SWTResourceManager.getFont("Arial", 15, SWT.NORMAL));
		toolkit.adapt(inputTextField_, true, true);

		inputTextField_.addKeyListener(new KeyAdapter() {

			public void keyPressed(KeyEvent e) {
				if (welcomeDisappear < 1) {
					inputTextField_.setText("");
					welcomeDisappear++;
				}

				if (e.keyCode == SWT.CR) {
					String feedback = "";
					String command = inputTextField_.getText();
					feedback = logic_.processCommand(command);
					inputTextField_.setText("");
					if (feedback.contains("<Searchy>")) {
						feedback = feedback.replaceFirst("<Searchy>", "");
						tableManager(commands.Search.searchResultList);
					} else if (feedback.contains("<helpy>!")) {
						feedback = feedback.replaceFirst("<helpy>!", "");
						helpMessage(display);
					} else if (feedback.contains("<Listy>!")) {
						feedback = feedback.replaceFirst("<Listy>!", "");
						tableManager(TaskMemoryController.getTasks());
					} else {
						tableManager(TaskDirector.fetchWelcomeListing());
					}
					outputTextField_.setText(feedback);
					// text1_.setText("hello World");
				}
			}
		});

		outputTextField_ = new Text(composite, SWT.BORDER | SWT.READ_ONLY
				| SWT.MULTI);
		FormData fd_text_1 = new FormData();
		fd_text_1.bottom = new FormAttachment(100, -10);
		fd_text_1.right = new FormAttachment(inputTextField_, 0, SWT.RIGHT);
		fd_text_1.top = new FormAttachment(0, 368);
		fd_text_1.left = new FormAttachment(0, 10);
		outputTextField_.setLayoutData(fd_text_1);
		outputTextField_.setFont(SWTResourceManager.getFont("Lucida Grande", 11,
				SWT.NORMAL));
		toolkit.adapt(outputTextField_, true, true);

		table_ = new Table(composite, SWT.BORDER | SWT.FULL_SELECTION);
		FormData fd_table = new FormData();
		fd_table.bottom = new FormAttachment(outputTextField_, -6);
		fd_table.top = new FormAttachment(inputTextField_, 12);
		fd_table.right = new FormAttachment(100, -10);
		table_.setLayoutData(fd_table);
		toolkit.paintBordersFor(table_);
		table_.setHeaderVisible(true);
		table_.setLinesVisible(true);
		TableColumn column_0 = new TableColumn(table_, SWT.NONE);
		column_0.setWidth(43);
		column_0.setText("Index");

		TableColumn column_1 = new TableColumn(table_, SWT.NONE);
		column_1.setWidth(191);
		column_1.setText("Title");

		TableColumn column_2 = new TableColumn(table_, SWT.NONE);
		column_2.setWidth(128);
		column_2.setText("Deadline");

		TableColumn column_3 = new TableColumn(table_, SWT.NONE);
		column_3.setText("Priority");
		column_3.setWidth(70);

		TableColumn column_4 = new TableColumn(table_, SWT.NONE);
		column_4.setWidth(100);
		column_4.setText("Status");

		Composite composite_1 = new Composite(composite, SWT.BORDER);
		fd_table.left = new FormAttachment(0, 261);
		FormData fd_composite_1 = new FormData();
		fd_composite_1.bottom = new FormAttachment(outputTextField_, -6);
		fd_composite_1.left = new FormAttachment(0, 10);
		fd_composite_1.right = new FormAttachment(table_, -6);
		fd_composite_1.top = new FormAttachment(inputTextField_, 12);
		composite_1.setLayoutData(fd_composite_1);
		toolkit.adapt(composite_1);
		toolkit.paintBordersFor(composite_1);
		composite_1.setLayout(new GridLayout(1, false));

		btnNewButton_ = new Button(composite_1, SWT.NONE);
		btnNewButton_.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				helpMessage(display);
			}
		});
		GridData gd_btnNewButton = new GridData(SWT.FILL, SWT.CENTER, true, false,
				1, 1);

		gd_btnNewButton.horizontalAlignment = GridData.CENTER;
		gd_btnNewButton.verticalAlignment = GridData.CENTER;
		gd_btnNewButton.widthHint = 150;
		btnNewButton_.setLayoutData(gd_btnNewButton);
		toolkit.adapt(btnNewButton_, true, true);
		btnNewButton_.setText("help");

		final DateTime dateTime = new DateTime(composite_1, SWT.BORDER);
		dateTime.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				DateTime datetime_1 = (DateTime) e.getSource();
				Calendar c = Calendar.getInstance();
				c.set(datetime_1.getYear(), datetime_1.getMonth(), datetime_1.getDay());
				Date date = c.getTime();
				tableManager(TaskMemoryController.filterByDate(date));
				Date date1 = new Date();
				outputTextField_.setText(e.getSource().toString());
			}
		});
		GridData gd_dateTime = new GridData(SWT.FILL, SWT.CENTER, true, false, 1, 1);

		gd_dateTime.horizontalAlignment = GridData.CENTER;
		gd_dateTime.verticalAlignment = GridData.CENTER;
		dateTime.setLayoutData(gd_dateTime);

		toolkit.adapt(dateTime);
		toolkit.paintBordersFor(dateTime);

		dateTime1_ = new DateTime(composite_1, SWT.BORDER | SWT.CALENDAR);
		GridData gd_dateTime_1 = new GridData(SWT.FILL, SWT.CENTER, true, false, 1,
				1);
		gd_dateTime_1.heightHint = 158;

		gd_dateTime_1.horizontalAlignment = GridData.CENTER;
		gd_dateTime_1.verticalAlignment = GridData.CENTER;
		dateTime1_.setLayoutData(gd_dateTime_1);
		dateTime1_.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseUp(MouseEvent e) {
				DateTime datetime_1 = (DateTime) e.getSource();
				Calendar c = Calendar.getInstance();
				c.set(datetime_1.getYear(), datetime_1.getMonth(), datetime_1.getDay());
				Date date = c.getTime();
				tableManager(TaskMemoryController.filterByDate(date));
				Date date1 = new Date();
				outputTextField_.setText(e.getSource().toString());
				dateTime.setDate(dateTime1_.getYear(), dateTime1_.getMonth(),
						dateTime1_.getDay());
			}
		});
		toolkit.adapt(dateTime1_);
		toolkit.paintBordersFor(dateTime1_);

	}

	public void tableManager(ArrayList<Task> taskList) {
		int numOfTasks = taskList.size();
		table_.removeAll();
		for (int i = 0; i < numOfTasks; i++) {
			TableItem tableItem = new TableItem(table_, SWT.NONE);
			tableItem.setText(0, "" + (i + 1));
			tableItem.setText(1, taskList.get(i).getTitle());
			tableItem.setText(2,
					DateFormat.getInstance().format(taskList.get(i).getDeadline()));
			tableItem.setText(3, taskList.get(i).getPriority().toString());
			tableItem.setText(4, taskList.get(i).getStatus().toString());
		}
		TaskMemoryController.taskTracking(taskList);
	}

	public void helpMessage(Display display) {

		Shell shell_1 = new Shell(display);
		Help help = new Help(shell_1, SWT.NONE);
		help.pack();
		shell_1.pack();
		shell_1.setText("Help");
		shell_1.open();

		while (!shell_1.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
	}

	public void IntialDisplay(TaskDirector welcomeList) {
		welcomeList.processCommand("list");
		inputTextField_
				.setText("Enter your command here; press \"help\" button for further info");
		outputTextField_.setText(WELCOME_MESSAGE);
		tableManager(TaskDirector.fetchWelcomeListing());
	}
}
